package com.wn.ziroombackend.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.wn.ziroombackend.entity.OrderClr;
import com.wn.ziroombackend.entity.vo.OrderIdClrVo;
import com.wn.ziroombackend.service.OrderClrService;
import com.wn.ziroombackend.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangzhijia
 * @since 2023-02-27 10:56:29
 */
@RestController
@RequestMapping("/orderClr")
public class OrderClrController {

    @Autowired
    OrderClrService orderClrService;


    @GetMapping("/selectOrderClrByPage")
    public R selectOrderClrByPage(@RequestParam(value = "pageNum")String pageNum,
                                  @RequestParam(value = "pageSize")String pageSize,
                                  @RequestParam(value = "orderIdClr",defaultValue = "")String orderIdClr,
                                  @RequestParam(value = "areasId",defaultValue = "")String areasId,
                                  @RequestParam(value = "streets_id",defaultValue = "")String streetsId,
                                  @RequestParam(value = "state",defaultValue = "")String state){


        PageInfo<OrderIdClrVo> pageInfo = orderClrService.selectOrderClrByPage(pageNum, pageSize,
                orderIdClr,areasId,streetsId,state);



        return R.ok("ok").data("pageInfo",pageInfo);
    }

    @GetMapping("/selectOneOrderClr")
    public R selectOneOrderClr(@RequestParam(value = "id")String id){
        OrderClr one = orderClrService.getOne(new QueryWrapper<OrderClr>().eq("id", id));
        System.out.println("修改单个的数据为:"+one);

        return R.ok("ok").data("obj",one);
    }

    @GetMapping("/editOneOrderClr")
    public R editOneOrderClr(@RequestParam(value = "id")String id,
                             @RequestParam(value = "areasId")String areasId,
                             @RequestParam(value = "streetsId")String streetsId,
                             @RequestParam(value = "address")String address,
                             @RequestParam(value = "BookDay")String BookDay,
                             @RequestParam(value = "starttime")String starttime,
                             @RequestParam(value = "endtime")String endtime){
        System.out.println("----------------");
        System.out.println(id);
        System.out.println(areasId);
        System.out.println(streetsId);
        System.out.println(address);
        System.out.println(BookDay);
        System.out.println(starttime);
        System.out.println(endtime);


        String result="";


        //开始时间和结束时间转为Date类
        String createTimeS = BookDay + " " + starttime + ":00";
        String endTimeS = BookDay + " " + endtime + ":00";

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            //最终时间参数
            Date createParse = format.parse(createTimeS);
            Date endParse = format.parse(endTimeS);

            System.out.println("date:" + createParse);
            System.out.println("date:" + endParse);

            OrderClr orderClr = new OrderClr();
            orderClr.setOrderIdClr("-1");

            //test
            orderClr.setId(Integer.parseInt(id));


            orderClr.setAreasId(areasId);
            orderClr.setStreetsId(streetsId);
            orderClr.setAddress(address);


            orderClr.setStarttime(createParse);
            orderClr.setEndtime(endParse);


            orderClr.setUpdatetime(new Date());

            boolean b = orderClrService.updateById(orderClr);

            return R.ok("ok");

        } catch (ParseException e) {
            e.printStackTrace();
        }
        result="请联系管理员!";
        return R.error(result);
    }
}

